{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. Writing a quantum program with QISKit\n",
    "\n",
    "QISKit is an open-source python library developed by IBM [1,2] to write quantum programs which run on IBM's quantum computer or on a simulator. QISKit allows you to build a quantum circuit of choice, using the available quantum gates, and run the algorithm implemented by the circuit a desired number of times. In this way, statistics can be collected and interpreted in the framework of quantum mechanics.\n",
    "\n",
    "Quantum programs are objects (in the programming language sense) which include quantum and classical bit registers and a quantum circuit.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.1 Template for running a quantum program\n",
    "\n",
    "To write a quantum algorithm in QISKit you can use the following template:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initialization\n",
    "\n",
    "We have created a library called \"initialize\" which simplifies the task of initializing a quantum program. The following code creates a class which allows the user to initialize any quantum algorithm. The user needs only to specify the following parameters: the name of the quantum circuit, the number of qubits needed, the number of bits needed to store the results of measurements, the type of backend where the algorithm is run (simulator or quantum computer) and the number of times the algorithm is to be run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from initialize import *\n",
    "\n",
    "my_algorithm = initialize(circuit_name = 'demo', qubit_number=2, bit_number=2, backend = 'local_qasm_simulator', shots = 1024)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Build the quantum circuit\n",
    "\n",
    "After initializing the circuit with the desired parameter, the user can then specify the sequence of quantum gates which make up the circuit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "x\n",
      "x\n",
      "h\n",
      "cx\n",
      "measure\n",
      "measure\n"
     ]
    }
   ],
   "source": [
    "#Append sequence of gates to the quantum circuit. \n",
    "#For each gate, the qubits on which it is acting must be specified along with other gate-dependent parameters\n",
    "\n",
    "my_algorithm.q_circuit.x(my_algorithm.q_reg[0]) # apply the X gate to the first qubit\n",
    "my_algorithm.q_circuit.x(my_algorithm.q_reg[1]) # apply the X gate to the second qubit\n",
    "my_algorithm.q_circuit.h(my_algorithm.q_reg[0]) # apply the Hadamard gate to the first qubit\n",
    "my_algorithm.q_circuit.cx(my_algorithm.q_reg[0],my_algorithm.q_reg[1]) # apply the CNOT gate using the first qubit as control and second qubit as target\n",
    "my_algorithm.q_circuit.measure(my_algorithm.q_reg[0], my_algorithm.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "my_algorithm.q_circuit.measure(my_algorithm.q_reg[1], my_algorithm.c_reg[1]) # measures the second qubit and store the result in the second bit\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_algorithm.q_circuit:\n",
    "    print(circuit.name)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Execute the algorithm\n",
    "\n",
    "Now that everything is ready, the user can run the quantum algorithm soecified by the steps above. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The measured outcomes of the circuits are: {'01': 508, '10': 516}\n"
     ]
    }
   ],
   "source": [
    "#Execute the quantum algorithm\n",
    "result = my_algorithm.Q_program.execute(my_algorithm.circ_name, backend=my_algorithm.backend, shots= my_algorithm.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "\n",
    "counts = result.get_counts(my_algorithm.circ_name) ## you can declare/initiate q_name first at the time you initialize circuit,\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Note: \n",
    "In the outcomes, qubits are ordered right to left. Therefore the right-most is the outcome of the measurement of the first qubit, the second from the right is for the second qubit, and so on."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <span style=\"color:blue\"> 5.2 Template for running a quantum program </span>\n",
    "\n",
    "Here, and in the rest of the lecture notes, we will use a blue colored title to denote sections with exercises which can be done in QISKit. Below, you will find a series of simple quantum programs written to show how to use QISKit to solve some specific problem.  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\"> QISKit: create multi-qubit state with superposition and entanglement </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 1) Create the multi-qubit state  $\\lvert 0101010 \\rangle$ </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "x\n",
      "x\n",
      "x\n",
      "measure\n",
      "measure\n",
      "measure\n",
      "measure\n",
      "measure\n",
      "measure\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'0101010': 1024}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'multi', qubit_number=7, bit_number=7, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.x(my_alg.q_reg[1]) # applies X gate to second qubit\n",
    "my_alg.q_circuit.x(my_alg.q_reg[3]) # applies X gate to fourth qubit\n",
    "my_alg.q_circuit.x(my_alg.q_reg[5]) # applies X gate to sixth qubit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[1], my_alg.c_reg[1]) # measures the second qubit and store the result in the second bit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[2], my_alg.c_reg[2]) # measures the third qubit and store the result in the third bit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[3], my_alg.c_reg[3]) # measures the fourth qubit and store the result in the fourth bit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[4], my_alg.c_reg[4]) # measures the fifth qubit and store the result in the fifth bit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[5], my_alg.c_reg[5]) # measures the sixth qubit and store the result in the sixth bit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[6], my_alg.c_reg[6]) # measures the seventh qubit and store the result in the seventh bit\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 2) Create the superposition state for two qubits  $ \\frac{1}{2}\\left( \\lvert 00 \\rangle + \\lvert 01 \\rangle + \\lvert 10 \\rangle + \\lvert 11 \\rangle \\right)$ </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "h\n",
      "h\n",
      "measure\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'00': 258, '01': 261, '10': 260, '11': 245}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'multi_super', qubit_number=2, bit_number=2, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit\n",
    "my_alg.q_circuit.h(my_alg.q_reg[1]) # applies X gate to second qubit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[1], my_alg.c_reg[1]) # measures the second qubit and store the result in the second bit\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 3) Create the entangled Bell  $ \\frac{1}{\\sqrt{2}}\\left( \\lvert 00 \\rangle + \\lvert 11 \\rangle \\right)$ </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "h\n",
      "cx\n",
      "measure\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'00': 515, '11': 509}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'bell', qubit_number=2, bit_number=2, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit\n",
    "my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[1]) # applies CX gate using the first qubit as control and the second qubit as target\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[1], my_alg.c_reg[1]) # measures the second qubit and store the result in the second bit\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\"> QISKit: Show effects of single and two qubit gates </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 4) Use the X gate </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "x\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'1': 1024}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'x_gate', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.x(my_alg.q_reg[0]) # applies X gate to first qubit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 5) Use the Y gate </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "y\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'1': 1024}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'y_gate', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.y(my_alg.q_reg[0]) # applies Y gate to first qubit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 6) Use the H gate </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "h\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'0': 540, '1': 484}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'h_gate', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 7) Use the $R_x$ gate </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "rx\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'0': 867, '1': 157}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'rx_gate', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "theta = 0.79 # angle of rotation pi/4\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.rx(theta, my_alg.q_reg[0]) # applies x-rotation of angle theta to first qubit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 8) Use the $CX_{12}$ gate </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "cx\n",
      "measure\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'00': 1024}\n",
      "List of gates:\n",
      "x\n",
      "cx\n",
      "measure\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'11': 1024}\n",
      "List of gates:\n",
      "x\n",
      "cx\n",
      "measure\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'10': 1024}\n",
      "List of gates:\n",
      "x\n",
      "x\n",
      "cx\n",
      "measure\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'01': 1024}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "for _ in range(4): # run over all possible initial states of two qubits\n",
    "\n",
    "    #initialize quantum program\n",
    "    my_alg = initialize(circuit_name = 'cx_gate', qubit_number=2, bit_number=2, backend = 'local_qasm_simulator', shots = 1024)\n",
    "    \n",
    "    #add gates to the circuit\n",
    "    if _ == 1:\n",
    "        my_alg.q_circuit.x(my_alg.q_reg[0]) # applies X gate to first qubit (prepares state 10)\n",
    "    if _ == 2:\n",
    "        my_alg.q_circuit.x(my_alg.q_reg[1]) # applies X gate to second qubit (prepares state 01)\n",
    "    if _ == 3:\n",
    "        my_alg.q_circuit.x(my_alg.q_reg[0]) # applies X gate to first qubit\n",
    "        my_alg.q_circuit.x(my_alg.q_reg[1]) # applies X gate to second qubit (prepares state 11)\n",
    "    \n",
    "    my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[1]) ## applies CX gate using the first qubit as control and the second qubit as target\n",
    "    my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "    my_alg.q_circuit.measure(my_alg.q_reg[1], my_alg.c_reg[1]) # measures the second qubit and store the result in the second bit\n",
    "\n",
    "    print('List of gates:')\n",
    "    for circuit in my_alg.q_circuit:\n",
    "        print(circuit.name)\n",
    "\n",
    "    #Execute the quantum algorithm\n",
    "    result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "    #Show the results obtained from the quantum algorithm \n",
    "    counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "    print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\"> QISKit: Time-evolution of the qubit's state in a quantum circuit (example in sec. 3.4)  </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 9) Time $t=t_1$  </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "id\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'0': 1024}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'evo_1', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.iden(my_alg.q_reg[0]) # applies U1\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 10) Time $t=t_2$  </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "id\n",
      "x\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'1': 1024}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'evo_2', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.iden(my_alg.q_reg[0]) \n",
    "my_alg.q_circuit.x(my_alg.q_reg[0]) # applies U2\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 11) Time $t=t_3$  </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "id\n",
      "x\n",
      "x\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'0': 1024}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'evo_3', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "my_alg.q_circuit.iden(my_alg.q_reg[0]) \n",
    "my_alg.q_circuit.x(my_alg.q_reg[0])\n",
    "my_alg.q_circuit.x(my_alg.q_reg[0]) # applies U3\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit\n",
    "\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "\n",
    "[1] https://github.com/QISKit\n",
    "\n",
    "[2] https://qiskit.org/documentation/\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
